#!/bin/bash

cmd="sh -x gradlew "
isPatch=false

if [ "$1" == "clean" ]; then
    cmd=${cmd}"clean"
    echo "执行清除构建缓存指令"
    eval $cmd
    exit
fi

if [ "$1" == "release" ]; then
    if [ "$2" == "channels" ]; then
        cmd=${cmd}"assembleReleaseChannels"
    else
        if [ "$2" == "" ]; then
            echo "请指定渠道名称！"
            exit
        else
            cmd=${cmd}"assembleReleaseChannels -PchannelList=$2"
        fi
    fi
elif [ "$1" == "debug" ]; then
    if [ "$2" == "channels" ]; then
        cmd=${cmd}"assembleDebugChannels"
    else
        if [ "$2" == "" ]; then
            echo "请指定渠道名称！"
            exit
        else
            cmd=${cmd}"assembleDebugChannels -PchannelList=$2"
        fi
    fi
elif [ "$1" == "patch" ]; then
    isPatch=true
    if [ "$2" == "debug" ]; then
        cmd=${cmd}"buildTinkerPatchDebug"
    else
        cmd=${cmd}"buildTinkerPatchRelease"
    fi
    currentDir=$(pwd)
    patchDir=${currentDir}"/app/build/outputs/patch"
    destDir=${currentDir}"/output/patch"
    cmd=${cmd}" && mkdir -p $destDir && cp -R $patchDir/* $destDir"
else
    echo "请指定Build Type (1、debug，2、release)"
    exit
fi

echo -e "\n\n\n-------------------------------------------------------\n"

if [ ${isPatch} == true ]; then
    echo " 准备执行生成补丁包命令：$cmd "
    echo " 基准包配置文件：tinker/config.xml"
else
    echo " 准备执行打包命令: $cmd"
fi

echo -e "\n-------------------------------------------------------\n\n\n"

eval $cmd